package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.VO.IdStaVO;
import com.itheima.Dto.SetmealDto;
import com.itheima.bean.Setmeal;
import com.itheima.common.PageParm;
import com.itheima.common.R;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Transactional
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 分页查询
     * @param param
     * @return
     */
    @GetMapping("/page")
    public R selectPage( PageParm param){
        IPage<SetmealDto> setmealDtoIPage = setmealService.selectPage(param);
        return  R.success(setmealDtoIPage);
    }

    /**
     * 新建套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
        int row = setmealService.insert(setmealDto);
        if(row>0)
            return R.success("新建套餐成功！！！");
        else return R.error("新建套餐失败！！！");
    }

    /**
     * 根据ID查询套餐所有信息
     * @param id
     * @return
     */
    @GetMapping
    public R selectById(long id){
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);
    }
    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R updateSetmealAndDish(@RequestBody SetmealDto setmealDto){
        int row = setmealService.updateSetmeal(setmealDto);
        if(row>0)
            return R.success("修改数据成功！！！");
        else return R.error("修改数据失败！！！");
    }

    /**
     * 批量删除套餐所有信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteByIds( @RequestParam List<Long> ids){
        int row = setmealService.deleteByIds(ids);
        if(row>0)
            return R.success("批量删除套餐成功！！！");
        else return R.error("批量删除失败！！！");
    }

    /**
     * 批量更新套餐状态信息
     *
     * @param idStaVO
     * @return
     */
    @PostMapping("/status")
    public R upStatus(@RequestBody IdStaVO idStaVO) {

        int update = setmealService.upStatus(idStaVO);
        System.out.println(update);
        if (update > 0)
            return R.success("修改状态成功！！！");
        else return R.error("修改状态失败！！！");
    }

    @GetMapping("/list")
    public R FindUseSetmeal(long categoryId){
        List<Setmeal> setmeals = setmealService.selectByCategoryIdUse(categoryId);
        return R.success(setmeals);

    }








}
